
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.checks.registry &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-checks-registry">
            
  <h1>Source code for django.core.checks.registry</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">from</span> <span class="nn">django.utils.itercompat</span> <span class="k">import</span> <span class="n">is_iterable</span>


<span class="k">class</span> <span class="nc">Tags</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Built-in tags for internal checks.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">admin</span> <span class="o">=</span> <span class="s1">&#39;admin&#39;</span>
    <span class="n">caches</span> <span class="o">=</span> <span class="s1">&#39;caches&#39;</span>
    <span class="n">compatibility</span> <span class="o">=</span> <span class="s1">&#39;compatibility&#39;</span>
    <span class="n">database</span> <span class="o">=</span> <span class="s1">&#39;database&#39;</span>
    <span class="n">models</span> <span class="o">=</span> <span class="s1">&#39;models&#39;</span>
    <span class="n">security</span> <span class="o">=</span> <span class="s1">&#39;security&#39;</span>
    <span class="n">signals</span> <span class="o">=</span> <span class="s1">&#39;signals&#39;</span>
    <span class="n">templates</span> <span class="o">=</span> <span class="s1">&#39;templates&#39;</span>
    <span class="n">urls</span> <span class="o">=</span> <span class="s1">&#39;urls&#39;</span>


<span class="k">class</span> <span class="nc">CheckRegistry</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">registered_checks</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deployment_checks</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="n">tags</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Can be used as a function or a decorator. Register given function</span>
<span class="sd">        `f` labeled with given `tags`. The function should receive **kwargs</span>
<span class="sd">        and return list of Errors and Warnings.</span>

<span class="sd">        Example::</span>

<span class="sd">            registry = CheckRegistry()</span>
<span class="sd">            @registry.register(&#39;mytag&#39;, &#39;anothertag&#39;)</span>
<span class="sd">            def my_check(apps, **kwargs):</span>
<span class="sd">                # ... perform checks and collect `errors` ...</span>
<span class="sd">                return errors</span>
<span class="sd">            # or</span>
<span class="sd">            registry.register(my_check, &#39;mytag&#39;, &#39;anothertag&#39;)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;deploy&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="n">check</span><span class="p">):</span>
            <span class="n">check</span><span class="o">.</span><span class="n">tags</span> <span class="o">=</span> <span class="n">tags</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;deploy&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">check</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">deployment_checks</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">deployment_checks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">check</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">check</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">registered_checks</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">registered_checks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">check</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">check</span>

        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">check</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">inner</span><span class="p">(</span><span class="n">check</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">check</span><span class="p">:</span>
                <span class="n">tags</span> <span class="o">+=</span> <span class="p">(</span><span class="n">check</span><span class="p">,</span> <span class="p">)</span>
            <span class="k">return</span> <span class="n">inner</span>

    <span class="k">def</span> <span class="nf">run_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_configs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">include_deployment_checks</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Run all registered checks and return list of Errors and Warnings.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">checks</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_checks</span><span class="p">(</span><span class="n">include_deployment_checks</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">tags</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">checks</span> <span class="o">=</span> <span class="p">[</span><span class="n">check</span> <span class="k">for</span> <span class="n">check</span> <span class="ow">in</span> <span class="n">checks</span>
                      <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">check</span><span class="p">,</span> <span class="s1">&#39;tags&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">set</span><span class="p">(</span><span class="n">check</span><span class="o">.</span><span class="n">tags</span><span class="p">)</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="n">tags</span><span class="p">)]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># By default, &#39;database&#39;-tagged checks are not run as they do more</span>
            <span class="c1"># than mere static code analysis.</span>
            <span class="n">checks</span> <span class="o">=</span> <span class="p">[</span><span class="n">check</span> <span class="k">for</span> <span class="n">check</span> <span class="ow">in</span> <span class="n">checks</span>
                      <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">check</span><span class="p">,</span> <span class="s1">&#39;tags&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">Tags</span><span class="o">.</span><span class="n">database</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">check</span><span class="o">.</span><span class="n">tags</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">check</span> <span class="ow">in</span> <span class="n">checks</span><span class="p">:</span>
            <span class="n">new_errors</span> <span class="o">=</span> <span class="n">check</span><span class="p">(</span><span class="n">app_configs</span><span class="o">=</span><span class="n">app_configs</span><span class="p">)</span>
            <span class="k">assert</span> <span class="n">is_iterable</span><span class="p">(</span><span class="n">new_errors</span><span class="p">),</span> <span class="p">(</span>
                <span class="s2">&quot;The function </span><span class="si">%r</span><span class="s2"> did not return a list. All functions registered &quot;</span>
                <span class="s2">&quot;with the checks registry must return a list.&quot;</span> <span class="o">%</span> <span class="n">check</span><span class="p">)</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new_errors</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">tag_exists</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">include_deployment_checks</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">tag</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tags_available</span><span class="p">(</span><span class="n">include_deployment_checks</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tags_available</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">deployment_checks</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">check</span><span class="o">.</span><span class="n">tags</span> <span class="k">for</span> <span class="n">check</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_checks</span><span class="p">(</span><span class="n">deployment_checks</span><span class="p">)</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">check</span><span class="p">,</span> <span class="s1">&#39;tags&#39;</span><span class="p">)]))</span>

    <span class="k">def</span> <span class="nf">get_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_deployment_checks</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">checks</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">registered_checks</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">include_deployment_checks</span><span class="p">:</span>
            <span class="n">checks</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">deployment_checks</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">checks</span>


<span class="n">registry</span> <span class="o">=</span> <span class="n">CheckRegistry</span><span class="p">()</span>
<span class="n">register</span> <span class="o">=</span> <span class="n">registry</span><span class="o">.</span><span class="n">register</span>
<span class="n">run_checks</span> <span class="o">=</span> <span class="n">registry</span><span class="o">.</span><span class="n">run_checks</span>
<span class="n">tag_exists</span> <span class="o">=</span> <span class="n">registry</span><span class="o">.</span><span class="n">tag_exists</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>